<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Provinder_model extends WST_Model {
	private $_table = "provinder";

	function __contruct(){
        parent::__construct();
    }
    public function all_provinder($flag=false){
        $all_provinder = $this->memcached->get('all_provinder');
        if($all_provinder===false || $flag===true){
            $this->db->order_by('orderNo','ASC');
            $query = $this->db->get($this->_table);
            $all_provinder = $query->result_array();
            //$query->free_result();
            $this->memcached->add('all_provinder', $all_provinder,null);
        }
        return $all_provinder;
    }
    public function getAllProvinder($flag=false){
        $all_provinder = $this->memcached->get('all_provinder');
        if($all_provinder===false || $flag===true){
            $this->db->order_by('orderNo','ASC');
            $query = $this->db->get($this->_table);
            $all_provinder = $query->result_array();
            $query->free_result();
            $this->memcached->add('all_provinder', $all_provinder,null);
        }
        return $all_provinder;
    }
    public function getSingle($id=null,$flag=false){
        $provinder_id = $this->memcached->get('provinder_'.$id);
        if($provinder_id===false || $flag===true){
            $this->db->where('id',$id);
            $query = $this->db->get($this->_table);
            $provinder_id = $query->row_array();
            $query->free_result();
            $this->memcached->add('provinder_'.$id, $provinder_id,null);
        }
        return $provinder_id;
    }

    public function count_all(){
        return $this->db->count_all($this->_table);
    }

    function find_all($param=array(),$current_page = 1, $page_size = 30){

        $where =" WHERE 1=1";
        if(count($param)>0){
            if(!empty($param['keyword'])){
                $where.=" AND name LIKE '%".$param['keyword']."%'";
            }
            if(!empty($param['parent'])){
                $where.=" AND parent = ".$param['parent']."";
            }
            if(!empty($param['child'])){
                $where.=" AND  parent = ".$param['child']." AND level= ".$param['parent']." ";
            }
        }

        $sql = "select * from ".$this->_table. $where."   order by orderNo ASC";

        if ($current_page > 0 && $page_size > 0) {
            $offset = ($current_page-1)* $page_size;
            $sql .= " limit {$offset}, {$page_size}";
        }else {
            $sql .= " limit {$page_size}";
        }
        $query = $this->db->query($sql);
        $data = $query->result_array();
        $query->free_result();
        return $data;
    }

	function get_url_params($page_key = "offset", $page_size_key = "page_size")
	{
		$items = array();
		foreach ($_GET as $key => $value) {
			if ($key != "paging" && $key != $page_key && $key != $page_size_key) {
				$items[] = $key . '=' . urlencode($value);
			}
	    }
	    if (count($items) > 0) {
	    	return "?paging=1&" . join("&", $items);
	    }
	    return '?paging=1';
	}

	public function getListAll(){
		$sql = "SELECT * FROM ".$this->_table." ORDER BY orderNo ASC ";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function getListParent($level=0){
		$sql = "SELECT id,name FROM ".$this->_table." WHERE level={$level} AND status=1 ORDER BY orderNo ASC ";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function getSubLevel($id){
		$sql = "SELECT id,name FROM ".$this->_table." WHERE parent={$id} AND status=1 ORDER BY orderNo ASC";
		$query = $this->db->query($sql);
		return $query->result_array();
	}

	public function insert($data=array()){

		if($this->db->insert($this->_table,$data)){
			return $this->db->insert_id();
		}
		else {
			return FALSE;
		}
	}

	public function update($data,$id=null){
		$this->db->where("id",$id);
		if($this->db->update($this->_table,$data))
			return TRUE;
		else
			return FALSE;
	}

}
